﻿Imports System.IO
Imports System.Drawing.Imaging
Imports System.Text
Imports System.Xml.Serialization

Public Class FormMain
	Enum states
		positive = 0
		negative = 1
		hourglass = 2
	End Enum

	Friend allDesc() As FieldsDescriptor

	Private Sub btnExtract_Click(sender As Object, e As EventArgs) Handles btnExtract.Click
		Dim currFile As FileInfo

		Try
			Call setStatus(states.hourglass, "Processing, please wait...", "Data extraction will took a few seconds, plese wait a moment")
			ssMain.Refresh()

			lvSource.Items.Clear()
			pbExifPreview.Image = Nothing
			pbThumPreview.Image = Nothing
			'lblExif.Text = "[no valid image data]"
			'lblThumb.Text = "[no valid image data]"

			Call setStatus(states.positive, "Processing, please wait...", "Processing, please wait...")

			'currFile = New FileInfo(Path.Combine(txtFolder.Text, lbFiles.SelectedItem.ToString))
			currFile = New FileInfo(txtSource.Text)
			If currFile.Exists Then
				Call getExif(currFile)
			Else
				Call setStatus(states.negative, "File is invalid", "File is invalid")
			End If
		Catch ex As Exception
			Call setStatus(states.negative, "Exception, hover for more info", ex.ToString)
		End Try
	End Sub
	Sub setStatus(ByVal WhatToSet As states, ByVal Title As String, ByVal Message As String)
		statStatus.Text = title
		statStatus.Image = ilBar.Images.Item(whatToSet)
		statStatus.ToolTipText = message
	End Sub
	Sub getExif(ByVal ImagePath As FileInfo)
		Dim Image As New Bitmap(ImagePath.FullName)
		Dim Properties As PropertyItem()
		Dim Encoder As New UTF8Encoding
		Dim Conversion As New DataConverter
		Dim RowProp As ListViewItem
		Dim ExifDataStream As New System.IO.FileStream("..\..\ExifData.xml", IO.FileMode.Open)

		Try
			Call deseria(ExifDataStream)
			properties = image.PropertyItems

			For Each currProp As PropertyItem In properties
				'COLUMN
				RowProp = New ListViewItem(currProp.Id)
				'COLUMN
				'RowProp.SubItems.Add(DecToHex(currProp.Id))

				Select Case currProp.Id
					Case 5
						'MessageBox.Show(BitConverter.ToString(currProp.Value, 0, currProp.Len))
					Case 20507
						Call createImage(currProp.Value)
				End Select

				'COLUMN
				'rowProp.SubItems.Add(currProp.Type)
				'COLUMN
				'rowProp.SubItems.Add(currProp.Len)

				For Each currDesc As FieldsDescriptor In allDesc
					If currDesc.Tag = DecToHex(currProp.Id) Then
						'COLUMN
						'rowProp.SubItems.Add(currDesc.Type)
						'COLUMN
						rowProp.SubItems.Add(currDesc.Desc)
						rowProp.ToolTipText = currDesc.Notes
						Exit For
					End If
				Next

				'COLUMN
				Select Case currProp.Type
					Case 1
						'rowProp.SubItems.Add(conversion.FormatTagByte(currProp, DataConverter.FormatInstr.Base64))
						RowProp.SubItems.Add(BitConverter.ToString(currProp.Value, 0, currProp.Len))
					Case 2
						RowProp.SubItems.Add(Conversion.FormatTagAscii(currProp))
					Case 3
						rowProp.SubItems.Add(conversion.FormatTagShort(currProp))
					Case 4
						rowProp.SubItems.Add(conversion.FormatTagLong(currProp))
					Case 5
						rowProp.SubItems.Add(conversion.FormatTagRational(currProp, DataConverter.FormatInstr.Fraction))
					Case 6
						rowProp.SubItems.Add(conversion.FormatTagUndefined(currProp, DataConverter.FormatInstr.AllChar))
					Case 7
						rowProp.SubItems.Add(conversion.FormatTagUndefined(currProp, DataConverter.FormatInstr.No_Op))
					Case 9, 10
						rowProp.SubItems.Add(conversion.FormatTagSRational(currProp, DataConverter.FormatInstr.Fraction))
				End Select

				lvSource.Items.Add(rowProp)
			Next

			setStatus(states.hourglass, "Sorting...", "Sorting...")
			lvSource.Sorting = SortOrder.Ascending
			lvSource.Sort()

			setStatus(states.positive, "Process completed", "Items processed: " & lvSource.Items.Count)
			pbThumPreview.Image = New Bitmap(image)
		Catch ex As Exception
			Call setStatus(states.negative, "Exception, hover for more info", ex.ToString)
		Finally
			image.Dispose()
			ExifDataStream.Close()
		End Try
	End Sub
	Sub deseria(ByVal fs As FileStream)
		Dim deser As XmlSerializer

		Try
			deser = New XmlSerializer(GetType(FieldsDescriptor()))
			allDesc = CType(deser.Deserialize(fs), FieldsDescriptor())
		Catch ex As Exception

			Call setStatus(states.negative, "Exception, hover for more info", ex.ToString)
		End Try
	End Sub
	Sub createImage(ByVal imageData As Byte())
		Dim image As Bitmap
		Dim imageStream As New MemoryStream(imageData)

		image = New Bitmap(imageStream)
		pbExifPreview.Image = image

		'TODO Check this
		'MessageBox.Show("Width : " & pbExifPreview.Image.Width & Environment.NewLine & "Height : " & pbExifPreview.Height)
		'lblExif.Text = "Width : " & pbExifPrew.Image.Width & Environment.NewLine & "Height : " & pbExifPrew.Height

	End Sub
End Class